import React from 'react';

import router from 'umi/router';
//import Link from 'umi/link'

import { connect } from 'dva';

import {  Button,Form, Input, InputNumber, Select } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;


@connect(({
  accountInvoice, accountInvoiceTax, accountAccount,

}) => ({
  accountInvoice, accountInvoiceTax, accountAccount,
}))
@Form.create()
class FormCreate extends React.Component {
  state = {
  }

  id = 0
  invoice_id=0

  async componentDidMount() {
    const { location: {query: { id, invoice_id } } } = this.props;

    const { dispatch } = this.props;

    if(invoice_id) {
      this.invoice_id = Number(invoice_id)
    }

    if (id) {
      this.id = Number(id)
      await dispatch({
        type: 'accountInvoiceTax/view',
        payload:  { id:  this.id }
      });

      const { accountInvoiceTax: {record} } = this.props;

      console.log(record)

      this.invoice_id = record.invoice_id && record.invoice_id.id;

    }

    await dispatch({
        type: 'accountInvoice/view',
        payload:  { id:  this.invoice_id }
    });

    await dispatch({
      type: 'accountAccount/search',
      payload: {domain:[] },
    });


  }


  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();


    const create_or_write = async (vals) => {
      const {accountInvoice:  {record: invoice}} = this.props;
      if(this.id){
        await dispatch({
          type: 'accountInvoice/write',
          payload : {
            id: invoice.id,
            vals: { tax_line_ids: [[1, this.id, vals]] },
          },
        });
      }

      router.push(`/AccountInvoice/Invoice?id=${this.invoice_id}`);

    }


    form.validateFieldsAndScroll( async (err, values) => {
      if (!err) {

        const vals = {
          ...values,
        };

        console.log( vals );

        await create_or_write(vals)

      }
    })
  }


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { accountInvoiceTax = {} } = this.props

    const { record={} } = accountInvoiceTax

    console.log(record)

    const { accountAccount: { recordsList: accountList } } = this.props;


    return (
      <div>
          <Form onSubmit={this.handleSubmit} >
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="说明">
              {getFieldDecorator('name', {
                //rules: [{ required: true }],
                //initialValue: record.name,
              })(
                <Input/>
              )}
            </FormItem>

            <div> 税: {record.tax_id && record.tax_id.name} </div>



            <FormItem label="科目">
              {getFieldDecorator('account_id', {
                //rules: [{ required: true }],
                initialValue: record.account_id ? record.account_id.id : 0
              })(
                <Select
                  placeholder="科目" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <div> 税金额: {record.amount } </div>

            <FormItem label="税调节">
              {getFieldDecorator('amount_rounding', {
                rules: [{ required: true }],
                initialValue: record.amount_rounding || 0,
              })(
                <InputNumber />
              )}
            </FormItem>


          </Form>


      </div>

    );
  }
}


export default FormCreate


